# scripts是定义接口测试用列.编测试用列脚本
# 导包
import requests
import unittest
import json
from api.login import LoginAPI
from parameterized import parameterized

def build_data():
    file="../data/login.json"
    test_data=[]
    with open(file,encoding="utf-8")as f:
        json_data=json.load(f)
        for case_data in json_data:
            username = case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            content_type = case_data.get("content_type")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((username, password, verify_code, content_type, status_code, status, msg))
            print(test_data)
        return test_data


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    def tearDown(self):
        if self.session:
             self.session.close()

    @parameterized.expand(build_data())
    def test01_login(self,username, password, verify_code, content_type, status_code, status, msg):
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code, response.status_code)
        self.assertIn(content_type, response.headers.get("Content-Type"))
        response = self.login_api.login(self.session, username, password, verify_code)
        print(response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get("status"))
        self.assertIn(msg, response.json().get("msg"))




#  设置前置处理
#  设置后置处理
# 创建测试方法
# 导包
# import requests
# import unittest
# from api.login import LoginAPI
#
# class TestLoginAPI(unittest.TestCase):
#     def setUp(self):
#         self.login_api = LoginAPI()
#         self.session = requests.Session()
#
#     def tearDown(self):
#         if self.session:
#             self.session.close()
# # 定义测试方法
# # 登录成功
# # 5 集成测试报告
#     def test01_login_success(self):
#
#         response = self.login_api.get_verify_code(self.session)
#         # 断言
#         self.assertEqual(200, response.status_code)
#         self.assertIn("image", response.headers.get("Content-Type"))
#         # 调用登录接口
#         response = self.login_api.login(self.session, "18371912089", "123456","8888")
#         print(response.json())
#         self.assertEqual(200, response.status_code)
#         self.assertEqual(1, response.json().get("status"))
#         self.assertIn("登陆成功", response.json().get("msg"))
#     # 账号不正确
#     def test02_user_is_not_exist(self):
#
#         response = self.login_api.get_verify_code(self.session)
#         # 断言
#         self.assertEqual(200, response.status_code)
#         self.assertIn("image", response.headers.get("Content-Type"))
#     # 调用登录接口
#         response = self.login_api.login(self.session, "1837191208", "123456","8888")
#         print(response.json())
#         self.assertEqual(200, response.status_code)
#         self.assertEqual(-1, response.json().get("status"))
#         self.assertIn("账号不存在", response.json().get("msg"))
#
#     def test03_password_exist(self):
#
#         response = self.login_api.get_verify_code(self.session)
#         # 断言
#         self.assertEqual(200, response.status_code)
#         self.assertIn("image", response.headers.get("Content-Type"))
#         # 调用登录接口
#         response = self.login_api.login(self.session, "18371912089", "error","8888")
#         print(response.json())
#         self.assertEqual(200, response.status_code)
#         self.assertEqual(-2, response.json().get("status"))
#         self.assertIn("密码错误", response.json().get("msg"))
